package com.frxuexi.mine.view

import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.UiState
import com.frxuexi.mine.intent.UserIntent
import com.frxuexi.mine.viewmodel.UserViewModel

@Composable
fun RegisterPage(navHostController: NavHostController,vm:UserViewModel= hiltViewModel()) {
    var name by remember {
        mutableStateOf("")
    }
    var no by remember {
        mutableStateOf("")
    }
    var phone by remember {
        mutableStateOf("")
    }
    var pid by remember {
        mutableStateOf("")
    }

    var pname by remember {
        mutableStateOf("")
    }

    
    LaunchedEffect(key1 = "", block = {
        vm.state.collect{
            when(it){
                is UiState.onSuccess<*> ->{
                    Log.i("===","注册成功")
                    navHostController.popBackStack()
                }

                else -> {}
            }
        }

    })

    Column(Modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(20.dp)) {
        val modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 20.dp)


        BaseTitle(title = "添加员工", navHostController = navHostController)
        Spacer(modifier = Modifier.height(20.dp))
        OutlinedTextField(value = name, modifier = modifier, onValueChange = {name=it}, label = { Text(text = "请输入用户名")})
        OutlinedTextField(value = no,  modifier = modifier, onValueChange = {no=it}, label = { Text(text = "请输入工号")})
        OutlinedTextField(value = phone, modifier = modifier,  onValueChange = {phone=it}, label = { Text(text = "请输入手机号")})
        OutlinedTextField(value = pid,  modifier = modifier, onValueChange = {pid=it}, label = { Text(text = "请输职位号")})
        OutlinedTextField(value = pname, modifier = modifier,  onValueChange = {pname=it}, label = { Text(text = "请输位置名称")})
        Button( modifier = modifier, onClick = {

           val map = mapOf<String, Any>(
               "name" to name,
               "no" to no,
               "sex" to "男",
               "birthday" to "2005-12-23",
               "phone" to phone,
               "did" to 2,
               "pid" to pid,
               "pname" to pname
           )
            vm.sendIntent(UserIntent.register(map))
        }) {
            Text(text = "添加")
        }
    }
}